package com.westar.base.businessUtil;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.westar.core.conf.SysConst;
import com.westar.base.exceptions.LoginException;
import com.westar.base.model.Department;
import com.westar.base.util.WestarHttpUtil;
import com.westar.core.dao.BaseDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DepartmentPageUtil {

    private static Logger logger = LoggerFactory.getLogger((BaseDao.class.getName()));


    /**
     * 取得所有区域数据信息
     * @param actCookie
     * @return
     * @throws IOException
     * @throws LoginException
     */
    public static List<Department> queryAllDep(String actCookie) throws IOException {
        List<Department> areaList = new ArrayList<>();

        String url = "http://"+ SysConst.DOMAIN+"/dataui/department/3.0/listData";

        Map<String,String> searchForm = new HashMap<>( );
        searchForm.put( "page","1" );
        searchForm.put( "rows","10" );

        String responeBody = WestarHttpUtil.postMethod( url,actCookie,searchForm);
        JSONObject res = JSONObject.parseObject(responeBody);
        Integer total = res.getInteger("total");
        if(total > 0){
            List<Department> areaSubList = DepartmentPageUtil.getDepFromPage(res,url,actCookie);
            areaList.addAll(areaSubList);
            Integer totalPageNum = total/10 + 1;
            for(int i=2;i<=totalPageNum;i++){
                searchForm.put( "page",i+"" );
                searchForm.put( "rows","10" );

                responeBody = WestarHttpUtil.postMethod( url,actCookie,searchForm);
                res = JSONObject.parseObject(responeBody);
                areaSubList = DepartmentPageUtil.getDepFromPage(res,url,actCookie);
                areaList.addAll(areaSubList);
            }

        }
        return areaList;
    }

    private static List<Department> getDepFromPage(JSONObject res, String url, String token)
            throws IOException {
        List<Department> areaList = new ArrayList<>();
        JSONArray rows = JSONArray.parseArray( res.getString( "rows" ));
        if(null != rows && !rows.isEmpty()){
            for (Object data :rows){
                JSONObject departmentJson = JSONObject.parseObject(data.toString());
                Department department = new Department();

                department.setDepId(departmentJson.getString( "idForStr" )  );

                Integer parentDeptId = departmentJson.getInteger( "parentDeptId" );
                if(null == parentDeptId){
                    parentDeptId = -1;
                }
                department.setParentDeptId(parentDeptId);

                department.setAreaId(departmentJson.getString( "areaId"));
                department.setDeptName(departmentJson.getString( "deptName"));
                department.setDeptSimpleName(departmentJson.getString( "deptSimpleName"));
                department.setUpdateTime(departmentJson.getString( "updateTime"));

                areaList.add(department);

                String state = departmentJson.getString( "state");
                if(state.equals( "closed" )){
                    List<Department> areaSubList = DepartmentPageUtil.getDepSubFromPage(url,token,department.getAreaId());
                    areaList.addAll(areaSubList);
                }
            }
        }
        logger.info( "部门初始化完成" );
        return areaList;

    }

    private static List<Department> getDepSubFromPage(String url, String actCookie, String parentId)
            throws IOException {
        List<Department> areaList = new ArrayList<>();
        Map<String,String> searchForm = new HashMap<>( );
        searchForm.put( "page","1" );
        searchForm.put( "rows","10" );
        searchForm.put( "id",parentId.toString() );

        String responeBody = WestarHttpUtil.postMethod( url,actCookie,searchForm);
        JSONArray rows = JSONArray.parseArray( responeBody);
        if(null != rows && !rows.isEmpty()){
            for (Object data :rows){
                JSONObject departmentJson = JSONObject.parseObject(data.toString());
                Department department = new Department();

                department.setDepId(departmentJson.getString( "idForStr" )  );
                department.setParentDeptId(departmentJson.getInteger( "parentDeptId" ));
                department.setAreaId(departmentJson.getString( "areaId"));
                department.setDeptName(departmentJson.getString( "deptName"));
                department.setDeptSimpleName(departmentJson.getString( "deptSimpleName"));
                department.setUpdateTime(departmentJson.getString( "updateTime"));

                areaList.add(department);

                String state = departmentJson.getString( "state");
                if(state.equals( "closed" )){
                    List<Department> areaSubList = DepartmentPageUtil.getDepSubFromPage(url,actCookie,department.getAreaId());
                    areaList.addAll(areaSubList);
                }

            }
        }
        return areaList;
    }
}
